package com.example.mygrade.webapi;

import com.example.mygrade.core.PageUtils;
import com.example.mygrade.core.RUtil;
import com.example.mygrade.domain.SC;
import com.example.mygrade.service.SCService;
import com.example.mygrade.vo.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.domain.*;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping("/webapi/sc")
public class SCRestController {
    @Resource
    private SCService scService;

    /**
     * 读取得url:"/webapi/sc/list"
     */
    @GetMapping("/list")
    public List<SC> getAll(){
        List<SC> sc = scService.findAll();
        return sc;
    }

    /**
     * 分页读取全部内容:"/webapi/sc/getbypage"
     * @return
     */
    @GetMapping("/getbypage")
    public R getByPage(@RequestParam(value = "page",defaultValue="0") Integer page,
                               @RequestParam(value = "size",defaultValue = "10") Integer size,
                               @RequestParam(value = "sno",defaultValue = "") String sno,
                               @RequestParam(value = "cno",defaultValue = "") String cno
    ){
        Page<SC> scPage;
        Sort sort = Sort.by(Sort.Direction.ASC,"id");
        if(ObjectUtils.isEmpty(sno)  && ObjectUtils.isEmpty(cno)){
            Pageable pageable = PageRequest.of(page,size, sort);
            scPage = scService.findAll(pageable);
        }else if(ObjectUtils.isEmpty(sno) && !ObjectUtils.isEmpty(cno)){
            SC sc = new SC();
            sc.setCno(cno);
            Pageable pageable = PageRequest.of(page,size, sort);
            ExampleMatcher matcher = ExampleMatcher.matching()
                    //对课程号的精确查询
                    .withMatcher("cno",ExampleMatcher.GenericPropertyMatchers.contains())
                    ;
            Example<SC> example = Example.of(sc,matcher);
            scPage = scService.findAll(example,pageable);
        }else if(!ObjectUtils.isEmpty(sno) && ObjectUtils.isEmpty(cno)){
            SC sc = new SC();
            sc.setSno(sno);
            Pageable pageable = PageRequest.of(page,size, sort);
            ExampleMatcher matcher = ExampleMatcher.matching()
                    //对学号的模糊查询
                    .withMatcher("sno",ExampleMatcher.GenericPropertyMatchers.contains())
                    ;
            Example<SC> example = Example.of(sc,matcher);
            scPage = scService.findAll(example,pageable);
        }
        else{
            SC sc = new SC();
            sc.setSno(sno);
            sc.setCno(cno);
            Pageable pageable = PageRequest.of(page,size, sort);
            ExampleMatcher matcher = ExampleMatcher.matching()
                    //对学号的模糊查询
                    .withMatcher("sno",ExampleMatcher.GenericPropertyMatchers.contains())
                    //对课程号的精确查询
                    .withMatcher("cno",ExampleMatcher.GenericPropertyMatchers.contains())
                    ;
            Example<SC> example = Example.of(sc,matcher);
            scPage = scService.findAll(example,pageable);
        }

        PageUtils pageUtils = new PageUtils(scPage.getContent(),scPage.getTotalElements());
        return RUtil.success(pageUtils,"获取成功");
    }



    /**
     * 修改之前获取学生信息，读取的url:/webapi/sc/get
     * @return
     */
    @GetMapping("/get/{id}")
    public R get(@PathVariable Integer id){
        SC sc = scService.findById(id);
        return RUtil.success(sc,"获取成功");
    }

    /**
     * 新增学生的方法/webapi/sc/insert
     * @param sc
     * @return
     */
    @PostMapping("/insert")
    public R insert(SC sc){
        SC sc1 = scService.insert(sc);
        return RUtil.success(sc1,"插入成功");
    }

    /**
     * 更新学生的方法/webapi/sc/update
     * @param sc
     * @return
     */
    @PutMapping("/update")
    public R update(SC sc){
        SC sc1 = scService.update(sc);
        return RUtil.success(sc1,"更新成功");
    }

    /**
     * 删除学生的方法/webapi/sc/delete
     * @param id
     */
    @DeleteMapping("/delete/{id}")
    public R delete(@PathVariable Integer id){
        scService.delete(id);
        return RUtil.success(null,"删除成功");
    }

    /**
     * 批量删除学生
     * @param arr
     */
    @DeleteMapping("/batchdelete/{arr}")
    public R delete(@PathVariable Integer[] arr){

        scService.delete(arr);
        return RUtil.success(null,"批量删除成功");
    }

    /**
     * 批量导入成绩
     * @param file
     * @param session
     * @return
     * @throws Exception
     */
    @RequestMapping("/importSc")
    @ResponseBody
    public R importSc(@RequestParam MultipartFile[] file, HttpSession session) throws Exception{
        int result = 0;
        try {
            result = scService.insertSC(file[0]);
        }catch (Exception e){
            e.printStackTrace();
        }

        if (result > 0){
            return RUtil.success(result,"导入成功");
//            return "{\"result\":\"excel文件数据导入成功！\"}";
        }else{
//            return "{\"result\":\"excel文件数据导入失败！\"}";
            return RUtil.error(400,"导入失败");
        }
    }
}
